package com.easidea.tim.flex.ui.module.inventory
{
	
	import com.easidea.tim.flex.bean.commodity.Product;
	import com.easidea.tim.flex.bean.inventory.CheckInOut;
	import com.easidea.tim.flex.bean.inventory.CheckInOutItem;
	import com.easidea.tim.flex.bean.inventory.Storage;
	import com.easidea.tim.flex.util.StringUtils;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	
	
	/*
	 *  出入库的XML数据工具类，进行XML的基本操作
	 *
	 */
	public class CheckInOutXMLUtil
	{
		private var _xml:XML = null;
		public function CheckInOutXMLUtil(checkInOutItemsXML:XML)
		{
			_xml = checkInOutItemsXML;
		}
		
		
		public function getProductXMLExists(productId:String, name:String, colorCode:String, isSample:Boolean):XML{
			var productXMLList:XMLListCollection = new XMLListCollection(_xml.children());
			for(var i:int = 0;i<productXMLList.length;i++){
				var product:XML = productXMLList.getItemAt(i) as XML;
				var productNameXML:XML = new XMLListCollection(product.elements("name")).getItemAt(0) as XML;
				var productColorCodeXML:XML = new XMLListCollection(product.elements("colorCode")).getItemAt(0) as XML;
				var productSampleXML:XML = new XMLListCollection(product.elements("isSample")).getItemAt(0) as XML;
				if(product.@id != null && productNameXML != null && productColorCodeXML != null && productSampleXML != null){
					var nameValue:String = productNameXML.toString();
					var colorCodeValue:String = productColorCodeXML.toString();
					var sampleValue:Boolean = new Boolean((productSampleXML.toString() == "true" ? true:false));
					if(product.@id.toString() == productId && nameValue == name && colorCodeValue == colorCode && sampleValue ==  isSample ){
						return product;
					}
				}
			}
			return null;
		}
		
		public function getProductXMLIndex(productId:String, name:String, colorCode:String, isSample:Boolean):int {
			var productXMLList:XMLListCollection = new XMLListCollection(_xml.children());
			for(var i:int = 0;i<productXMLList.length;i++){
				var product:XML = productXMLList.getItemAt(i) as XML;
				var productNameXML:XML = new XMLListCollection(product.elements("name")).getItemAt(0) as XML;
				var productColorCodeXML:XML = new XMLListCollection(product.elements("colorCode")).getItemAt(0) as XML;
				var productSampleXML:XML = new XMLListCollection(product.elements("isSample")).getItemAt(0) as XML;
				var nameValue:String = productNameXML.toString();
				var colorCodeValue:String = productColorCodeXML.toString();
				var sampleValue:Boolean = new Boolean((productSampleXML.toString() == "true" ? true:false));
				if(product.@id != null && product.@id.toString() == productId && nameValue == name && colorCodeValue == colorCode && sampleValue ==  isSample ){
					return i;
				}
			}
			return -1;
		}
		
		public function insertProductXML(productId:String, name:String, colorCode:String , isSample:Boolean, ... meterNums) :void {
			var meterNumString:String = "";
			for(var i:int = 0; i<meterNums.length; i++){
				meterNumString+="<meterNum id=\"0\">"+meterNums[i]+"</meterNum>";
			}
			var productXMLList:XMLListCollection = new XMLListCollection(_xml.children());
			productXMLList.addItem(new XML("<product id=\""+productId+"\"><name>"+name+"</name><colorCode>"+colorCode+"</colorCode><isSample>"+isSample+"</isSample><meterNums>"+meterNumString+"</meterNums></product>"));
			trace("after insert new product: " + _xml);
		}
		
		/**
		 * 切换剪样标志，如果传入的原始剪样标志的值为false，切换成true后，则会删除当前布品除第一个数量外的其余数量
		 */
		public function toggleSampleFlagXML(productId:String, name:String, colorCode:String , isSample:Boolean):Boolean{
			var productXML:XML = getProductXMLExists(productId,name,colorCode,isSample);
			if(productXML != null){
				var productSampleXML:XML = new XMLListCollection(productXML.elements("isSample")).getItemAt(0) as XML;
				if(!isSample){
					var productMetersListXML:XMLListCollection = new XMLListCollection(productXML.meterNums.children());
					for(var j:int = productMetersListXML.length - 1; j>0; j--){
						productMetersListXML.removeItemAt(j);
					}
				}

				var productElementList:XMLListCollection = new XMLListCollection(productXML.children());
				for(var i:int = 0; i<productElementList.length; i++){
					var tempXML:XML = productElementList.getItemAt(i) as XML;
					if(String(tempXML.name()) == "isSample"){
						productElementList.removeItemAt(i);
					}
				}				
				productSampleXML = new XML("<isSample>"+!isSample+"</isSample>");
				productXML.insertChildBefore(productXML.meterNums,productSampleXML);
				trace("toogle before: " + isSample + ", after product: " + productXML);
				return true;
			}
			return false;
		}
		
		public function removeProductXML(productId:String ,name:String, colorCode:String , isSample:Boolean):Boolean{
			var productIndex : int = getProductXMLIndex(productId,name,colorCode,isSample);
			if(productIndex != -1){
				var productXMLList:XMLListCollection = new XMLListCollection(_xml.children());
				productXMLList.removeItemAt(productIndex);
				return true;
			}
			return false;
		}
		
		/**
		 * 删除指定布品下的米数数据
		 */
		public function removeProductMeter(productId:String, name:String, colorCode:String, isSample:Boolean, meterNumId:String, value:String):Boolean {
			var productXML:XML = getProductXMLExists(productId, name,colorCode,isSample);
			var productMeterNumsList:XMLListCollection = new XMLListCollection(productXML.meterNums.children());
			var isProcessed:Boolean = false;
			for(var i:int = 0; i<productMeterNumsList.length; i++){
				var idValue:String = productMeterNumsList.getItemAt(i).@id.toString();
				var meterNumValue:String = productMeterNumsList.getItemAt(i).toString();
				if(idValue == meterNumId && meterNumValue == value){
					productMeterNumsList.removeItemAt(i);
					isProcessed = true;
				}
			}
			return isProcessed;
		}
		
		public function mergeMeterNumXML(productId:String, name:String, colorCode:String, isSample:Boolean, meterNumId:int, meterNum:Number):void {
			var productXML:XML = getProductXMLExists(productId, name,colorCode,isSample);
			if(productXML != null){
				var isProcessed:Boolean = false;
				var productMeterNumList:XMLListCollection = new XMLListCollection(productXML.meterNums.children());
				for(var i:int = 0;i<productMeterNumList.length;i++){
					trace("id: " +XML(productMeterNumList.getItemAt(i)).@id);
					var idValue:String  = XML(productMeterNumList.getItemAt(i)).@id[0].toString();
					if(idValue == meterNumId.toString() && idValue == "0"){
						//如果存在相同ID的记录
						productMeterNumList.removeItemAt(i);
						productMeterNumList.addItemAt(new XML("<meterNum id=\""+meterNumId+"\">"+meterNum+"</meterNum>"),i);
						isProcessed = true;
					}
				}
				if(!isProcessed){
					//不存在相同ID的数据
					productMeterNumList.addItem(new XML("<meterNum id=\""+ (meterNumId+1) +"\">"+meterNum+"</meterNum>"));
				}
			}
		}
		
		public function getMeterNumsByProductIndex(index:int):XMLList {
			var productXMLList:XMLListCollection = new XMLListCollection(_xml.children());
			var productXML:XML = productXMLList.getItemAt(index) as XML;
			return productXML.elements("meterNums");
			//var productMeternums:XMLListCollection = new XMLListCollection(productXMLList.elements("meterNums"));
			//return XML(productMeternums.getItemAt()).children();
		}
		
		/**
		 * 将XML数据转换成CheckInOut数据对象
		 * 
		 */
		public function XML2CIO(cio:CheckInOut):void{
			if(_xml != null && _xml.children() != null && cio != null){
				trace("_xml: " +_xml);
				var productList:XMLListCollection = new XMLListCollection(_xml.children());
				var cioTotalMeterNum:Number = 0;
				var cioTotalPiNum:int = 0;
				var cioItemList:ArrayCollection = new ArrayCollection();
				
				for(var i:int = 0; i < productList.length; i++){
					var productXML:XML = XML(productList.getItemAt(i));
					var product:Product = new Product();
					product.name = productXML.name.toString();
					product.id = productXML.@id.toString();
					product.colorCode = productXML.colorCode.toString();
					
					var productMeterNumsList:XMLListCollection = new XMLListCollection(productXML.meterNums.children());
					
					for(var j:int = 0; j<productMeterNumsList.length; j++){
						var storage:Storage = new Storage();
						storage.name = product.name;
						storage.sampleFlag = productXML.isSample.toString() == "true"?true:false;
						storage.product = product;
						storage.meterNum = new Number(productMeterNumsList.getItemAt(j).toString());
						cioTotalMeterNum = cioTotalMeterNum + storage.meterNum;
						cioTotalPiNum++;
						
						var cioItem:CheckInOutItem = new CheckInOutItem();
						cioItem.checkInOut = cio;
						cioItem.storage = storage;
						
						cioItemList.addItem(cioItem);
					}
				}
				
				cio.items = cioItemList;
				cio.piNumSummary = cioTotalPiNum;
				cio.meterNumSummary = cioTotalMeterNum;
			}
		}

		public function CIO2XML(cio:CheckInOut):XML{
			var _xml:XML = null;
			if(cio != null){
				var rtnXMLStr:String = "<items>";
				var itemList:ArrayCollection = cio.items;
				
				var productName:String = "";
				var productColorCode:String = "";
				var productIsSample:Boolean = null;
				
				var productXMLStr:String = "";
				var meterNumStr:String = "";
				for(var i:int = 0;i<itemList.length;i++){
					if(itemList.getItemAt(i) != null){
						var storage:Storage = itemList.getItemAt(i).storage as Storage;
						if(storage != null && storage.product != null){
							if(storage.product.name == productName && storage.product.colorCode == productColorCode && storage.sampleFlag == productIsSample){
								meterNumStr = meterNumStr + "<meterNum id=\""+i+"\">"+StringUtils.formatNumString((itemList.getItemAt(i).inputMeterNum as Number).toFixed(1))+"</meterNum>";
							}else{
								if (i!=0){
									//注入product信息
									productXMLStr = productXMLStr + meterNumStr + "</meterNums></product>"; 
									rtnXMLStr = rtnXMLStr.concat(productXMLStr);
								}
								
								productName = storage.product.name;
								productColorCode = storage.product.colorCode;
								productIsSample = storage.sampleFlag;
								productXMLStr = "<product id=\""+storage.product.id+"\"><name>"+ productName + "</name><colorCode>"+productColorCode+"</colorCode><isSample>" + productIsSample+"</isSample><meterNums>";
								meterNumStr = "<meterNum id=\""+i+"\">"+StringUtils.formatNumString((itemList.getItemAt(i).inputMeterNum as Number).toFixed(1))+"</meterNum>";

//								if(itemList.length == 1){
//									productXMLStr = productXMLStr + meterNumStr + "</meterNums></product>"; 
//									rtnXMLStr = rtnXMLStr.concat(productXMLStr);
//								}
							} 
						}
					}
				}
				
				productXMLStr = productXMLStr + meterNumStr + "</meterNums></product>"; 
				rtnXMLStr = rtnXMLStr.concat(productXMLStr);
				
				_xml = new XML(rtnXMLStr+"</items>");
				trace("CIO2XML: " + _xml);
			}
			return _xml;
		}

	}
}